﻿module Sustainalytics.Common.Admin {

    var mod = angular.module('Sustainalytics.Common.Admin');

    mod.factory('staticObjectsAdmin', ['$rootScope', 'notificationFactory', '$location', ($rootScope, notificationFactory, $location) => {
        var getMessageByInternalCode = internalCode => {
            switch (internalCode) {
                case 1000:
                    return "Another report already generating.";

                case 1001:
                    return "Generating the report failed - Please contact your Administrator";

                case 1002:
                    return "Total number of concurrent users per application reached!";

                case 1003:
                    return "Login failed";

                case 'IncorrectUsernamePasswordCombination':
                case 1004:
                    return "The combination of username and password is incorrect. Please check and try again.";

                case 1011:
                    return "An error has occurred while reseting passwords. Please try again later.";

                case 1013:
                    return "There was an error while retrieving the users. Please try again later.";

                case 1014:
                    return "There was an error while retrieving accounts. Please try again later.";

                case 1015:
                    return "There was an error while retrieving the selected user. Please try again later.";

                case 1016:
                    return "There was an error while retrieving the selected account. Please try again later.";

                //ip login crud admin errors
                case 1020:
                    return "The ip adress that were sent as input seems to be invalid, please contact the admin.";

                case 1021:
                    return "The IP entered is already in use by another account!";


                case 2001: // News_PublishNonDraftNews
                    return "Only items in status Draft can be published. Please check your selection and try again.";

                case 2002: // News_NewsIsNull
                    return "News is null.";

                case 2003: // News_NewsNotValid
                case 2004: // News_NewsNotValid_TitleNullOrEmpty
                    return "Please check the following field(s): <br/>Article title, <br/>Date, <br/>Region, <br/>Unpublished date, <br/>Body";

                case 2005: // News_NewsNotValid_TitleLength
                    return "Please make sure that you do not insert more than 200 characters with spaces in the Article Title field.";

                case 2006: // News_DoesNotExist
                    return "News does not exist.";

                case 2007: // News_DateCreated_NotValid
                case 2008: // News_UnpublishedDate_NotValid
                    return "Please enter the date as MM/DD/YYYY";

                case 2015: // News_DeleteNonDraftNews
                    return "Only items in status Draft can be deleted. Please check your selection and try again.";

                // insights errors
                case 2100:
                    return "Please select a sector";

                case 2101:
                    return "Please select a topic";

                case 2102:
                    return "Please type a title";

                case 2103:
                    return "Please make sure that you do not insert more than 200 characters with spaces in the Title of Publication field";

                case 2104:
                    return "Please select a publish date";

                case 2105:
                    return "Please enter the date as MM/DD/YYYY";

                case 2106:
                    return "Please select a un-publish date";

                case 2107:
                    return "Please upload a publication file, the supported file format is PDF, max. 10M in size";

                case 2108:
                    return "The supported file format is PDF, max. 10M in size";

                case 2109:
                    return "Please upload a image, the supported file formats are jpg or png of 200 x 251 pixels";

                case 2110:
                    return "The supported file formats are jpg or png of 200 x 251 pixels";

                case 2111:
                    return "Please type a summary";

                case 2112:
                    return "Please make sure that you do not insert more than 5000 characters with spaces in the Summary field";

                case 2113:
                    return "Unpublished date must be bigger than published date";

                case 2114:
                    return "Upload failed, plase try again";

                // governance company search
                case 6015:
                    return "You do not have the necessary rights to access this page. Please contact your client advisor for details.";

                // product involvement 
                case 2200:
                    return "File is not *.xls or *.xlsx";

                case 2210:
                    return "Upload file failed";

                //news, events and insights error
                case 3003:
                    return "You do not have the necessary rights to access this page. Please contact your client advisor for details.";

                case 9999:
                    return "You are not authorized to use the Administration section.";

                default:
                    return "An error has occurred. Please contact your administrator!";
            }
        };
        var staticObjects: IStaticObjects = {
            apiBaseUrl: "api/",
            authBaseUrl: "/auth",
            baseUrl: "/accountsserv",
            eventsServiceBaseUrl: "/eventsserv",
            getMessageByInternalCode: getMessageByInternalCode,
            getErrorMesageByStatusCode: (statusCode, data) => {
                switch (statusCode) {
                    case 401:
                        return "Unauthorized";
                        
                    case 500:
                        return data && data.code
                            ? getMessageByInternalCode(data.code)
                            : "An error has occurred. Please contact your administrator!";
                        
                    case 404:
                        return "Resource not found";
                        
                    case 400:
                        return "Bad request. Resource not found";
                        
                    default:
                        return "Unknown error";
                }
            },
            errorCallback: (error, status, internalCode) => {
                if (error != "") {
                    notificationFactory.error(error.exceptionMessage, error.message);
                } else {
                    notificationFactory.error("", staticObjects.getErrorMesageByStatusCode(status, internalCode));
                }
            },
            initSignalR: () => {
                if ($rootScope.authData && $rootScope.authData.access_token) {
                    $.connection.hub.qs = { "access_token": $rootScope.authData.access_token };
                    $.connection.hub.start()
                        .done(() => {
                    })
                        .fail(error => {
                        //alert(error.message);
                        notificationFactory.error("SignalR failed to initialize!\n" + error.message, "SignalR error");
                    });
                }
            },
            clearUserInfo: () => {
                $rootScope.userInfo = new UserInfoClass();
                $rootScope.authData = new Authentication.AuthData();
                staticObjects.resourcePermissions = "";
            },
            redirectToLogin: () => {
                if ($location.path() != '/login') {
                    $location.url('/login');
                    //if (!$rootScope.$$phase) { $rootScope.$apply(); }
                }
            },
            redirectToDefault: (redirectTo?: string) => {
                if (redirectTo) {
                    $location.url(redirectTo);
                }
                else {
                    if ($location.path() != '/') {
                        $location.url('/');
                        //if (!$rootScope.$$phase) { $rootScope.$apply(); }
                    }
                }
            },
            redirectToLink: (link) => {
                if ($location.path() != link) {
                    $location.url(link);
                }
            },
            resourcePermissions: "",
            authDataIsExpired: (authData: Sustainalytics.Authentication.AuthData) => {
                if (!authData.loginTime) {
                    return true;
                }
                var day = authData.loginTime.getDay();
                var month = authData.loginTime.getMonth();
                var year = authData.loginTime.getFullYear();
                var hours = authData.loginTime.getHours();
                var minutes = authData.loginTime.getMinutes();
                var seconds = authData.loginTime.getSeconds();

                var endDate = new Date(year, month, day, hours, minutes, seconds + authData.expires_in);////
                var now = new Date();

                if (endDate > now) {
                    return true;
                } else {
                    return false;
                }
            }
        };
        return staticObjects;
    }]);
}